package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarServiceDetailsMapper;
import com.ruoyi.system.domain.CarServiceDetails;
import com.ruoyi.system.service.ICarServiceDetailsService;
import com.ruoyi.system.service.ICarMerchantService;
import com.ruoyi.system.service.ICarServiceTypeService;
import com.ruoyi.system.domain.CarMerchant;
import com.ruoyi.system.domain.CarServiceType;

/**
 * 服务详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class CarServiceDetailsServiceImpl implements ICarServiceDetailsService 
{
    @Autowired
    private CarServiceDetailsMapper carServiceDetailsMapper;
    
    @Autowired
    private ICarMerchantService carMerchantService;
    
    @Autowired
    private ICarServiceTypeService carServiceTypeService;

    /**
     * 查询服务详情
     * 
     * @param id 服务详情主键
     * @return 服务详情
     */
    @Override
    public CarServiceDetails selectCarServiceDetailsById(Integer id)
    {
        CarServiceDetails details = carServiceDetailsMapper.selectCarServiceDetailsById(id);
        
        if (details != null) {
            try {
                if (details.getmId() != null) {
                    CarMerchant merchant = carMerchantService.selectCarMerchantById(details.getmId().longValue());
                    if (merchant != null) {
                        details.setMerchantName(merchant.getName());
                    }
                }
                
                if (details.getTypeId() != null) {
                    CarServiceType type = carServiceTypeService.selectCarServiceTypeById(details.getTypeId());
                    if (type != null) {
                        details.setTypeName(type.getName());
                    }
                }
            } catch (Exception e) {
                // 如果获取名称失败，设置默认值
                if (details.getMerchantName() == null) {
                    details.setMerchantName("未知");
                }
                if (details.getTypeName() == null) {
                    details.setTypeName("未知");
                }
            }
        }
        
        return details;
    }

    /**
     * 查询服务详情列表
     * 
     * @param carServiceDetails 服务详情
     * @return 服务详情
     */
    @Override
    public List<CarServiceDetails> selectCarServiceDetailsList(CarServiceDetails carServiceDetails)
    {
        List<CarServiceDetails> list = carServiceDetailsMapper.selectCarServiceDetailsList(carServiceDetails);
        
        // 填充商户名称和服务类别名称
        for (CarServiceDetails details : list) {
            try {
                if (details.getmId() != null) {
                    CarMerchant merchant = carMerchantService.selectCarMerchantById(details.getmId().longValue());
                    if (merchant != null) {
                        details.setMerchantName(merchant.getName());
                    }
                }
                
                if (details.getTypeId() != null) {
                    CarServiceType type = carServiceTypeService.selectCarServiceTypeById(details.getTypeId());
                    if (type != null) {
                        details.setTypeName(type.getName());
                    }
                }
            } catch (Exception e) {
                // 如果获取名称失败，设置默认值
                if (details.getMerchantName() == null) {
                    details.setMerchantName("未知");
                }
                if (details.getTypeName() == null) {
                    details.setTypeName("未知");
                }
            }
        }
        
        return list;
    }

    /**
     * 新增服务详情
     * 
     * @param carServiceDetails 服务详情
     * @return 结果
     */
    @Override
    public int insertCarServiceDetails(CarServiceDetails carServiceDetails)
    {
        return carServiceDetailsMapper.insertCarServiceDetails(carServiceDetails);
    }

    /**
     * 修改服务详情
     * 
     * @param carServiceDetails 服务详情
     * @return 结果
     */
    @Override
    public int updateCarServiceDetails(CarServiceDetails carServiceDetails)
    {
        return carServiceDetailsMapper.updateCarServiceDetails(carServiceDetails);
    }

    /**
     * 批量删除服务详情
     * 
     * @param ids 需要删除的服务详情主键
     * @return 结果
     */
    @Override
    public int deleteCarServiceDetailsByIds(Integer[] ids)
    {
        return carServiceDetailsMapper.deleteCarServiceDetailsByIds(ids);
    }

    /**
     * 删除服务详情信息
     * 
     * @param id 服务详情主键
     * @return 结果
     */
    @Override
    public int deleteCarServiceDetailsById(Integer id)
    {
        return carServiceDetailsMapper.deleteCarServiceDetailsById(id);
    }
}
